package com.awesome.common.util.device;

import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.DisplayCutout;
import android.view.WindowInsets;
import android.view.WindowManager;

import java.lang.reflect.Method;

import static android.content.Context.KEYGUARD_SERVICE;

/**
 * Author: JFangZ
 * Email: janezhang1901@163.com
 * Date: 2020/11/18 17:51
 * Description:屏幕相关工具类
 */
public class ScreenUtil {
    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/18 17:53
     * Description:dp 转为 px
     */
    public static int dp2px(Context context, float dp) {
        float density = context.getResources().getDisplayMetrics().density;
        return (int) (dp * density + 0.5f);
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/18 17:53
     * Description:sp 转为 px
     */
    public static int sp2px(Context context, float sp) {
        float scaleDensity = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (sp * scaleDensity + 0.5f);
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/18 17:53
     * Description:px 转为 dp
     */
    public static int px2dp(Context context, float px) {
        float density = context.getResources().getDisplayMetrics().density;
        return (int) (px / density + 0.5f);
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/18 17:53
     * Description:px 转为 sp
     */
    public static int px2sp(Context context, float px) {
        float scaleDensity = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (px / scaleDensity + 0.5f);
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/19 9:43
     * Description:获取屏幕宽度,不包括虚拟按键的宽度
     */
    public static int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/19 9:43
     * Description:获取屏幕高度,不包括虚拟按键的高度,异形屏(刘海屏,水滴屏,挖孔屏)还不包括异形高度
     */
    public static int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/19 9:43
     * Description:获取屏幕真实宽度,包括虚拟按键的高度
     */
    public static int getScreenRealWidth(Context context) {
        int realWidth = 0;
        WindowManager mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display mDisplay = mWindowManager.getDefaultDisplay();
        DisplayMetrics mDisplayMetrics = new DisplayMetrics();
        Class<?> clazz;
        try {
            clazz = Class.forName("android.view.Display");
            @SuppressWarnings("unchecked")
            Method method = clazz.getMethod("getRealMetrics", DisplayMetrics.class);
            method.invoke(mDisplay, mDisplayMetrics);
            realWidth = mDisplayMetrics.widthPixels;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return realWidth;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/19 9:43
     * Description:获取屏幕真实高度,包括虚拟按键的高度
     */
    public static int getScreenRealHeight(Context context) {
        int realHeight = 0;
        WindowManager mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display mDisplay = mWindowManager.getDefaultDisplay();
        DisplayMetrics mDisplayMetrics = new DisplayMetrics();
        Class clazz;
        try {
            clazz = Class.forName("android.view.Display");
            @SuppressWarnings("unchecked")
            Method method = clazz.getMethod("getRealMetrics", DisplayMetrics.class);
            method.invoke(mDisplay, mDisplayMetrics);
            realHeight = mDisplayMetrics.heightPixels;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return realHeight;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/7/17 17:50
     * Description:获取异形屏(刘海屏,水滴屏,挖孔屏)的异形高度,需在 UI 绘制完成后调用
     */
    public static int getSafeInsetTop(Activity activity) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return 0;
        }
        WindowInsets windowInsets = activity.getWindow().getDecorView().getRootWindowInsets();
        if (windowInsets == null) {
            return 0;
        }
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
            return 0;
        }
        DisplayCutout displayCutout = windowInsets.getDisplayCutout();
        if (displayCutout == null) {
            return 0;
        }
        return displayCutout.getSafeInsetTop();
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/7/17 17:50
     * Description:判断是否是竖屏
     */
    public static boolean isPortrait(Context context) {
        return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/7/17 17:50
     * Description:判断是否是锁屏状态
     */
    public static boolean isLockScreen(Context context) {
        KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(KEYGUARD_SERVICE);
        return keyguardManager != null && keyguardManager.inKeyguardRestrictedInputMode();
    }
}
